#include <iostream>
#include "MessageBus.h"

void MessageBus::attach(const std::string& topic, MessageHadler&& messageHadler)
{
    //messageHandlers_.emplace(topic, std::forward<MessageHadler>(messageHadler));
    messageHandlers_[topic] = messageHadler;
}

void MessageBus::remove(const std::string& topic)
{
    auto iter = messageHandlers_.find(topic);
    if(iter != messageHandlers_.end()){
        messageHandlers_.erase(topic);
        std::cout << "remove topic: " << topic << std::endl;
    }
}

void MessageBus::sendMsg(const std::string& topic, const std::string& msg)
{
    auto iter = messageHandlers_.find(topic);
    if(iter != messageHandlers_.end()){
        iter->second(msg);
        std::cout << "send topic: " << topic << " message: "<< msg << std::endl;
    }
}